package com.ruoyi.collegeClass.service.impl;

import java.util.List;

import com.ruoyi.collegeClass.domain.BwClass;
import com.ruoyi.collegeClass.domain.vo.CvInformationVo;
import com.ruoyi.collegeClass.domain.vo.SaveVo;
import com.ruoyi.collegeClass.domain.vo.SysUserRoleVo;
import com.ruoyi.collegeClass.mapper.BwClassMapper;
import com.ruoyi.common.core.utils.DateUtils;


import com.ruoyi.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.collegeClass.mapper.BwStudentMapper;
import com.ruoyi.collegeClass.domain.BwStudent;
import com.ruoyi.collegeClass.service.IBwStudentService;

/**
 * 学生Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-24
 */
@Service
public class BwStudentServiceImpl implements IBwStudentService 
{
    @Autowired
    private BwStudentMapper bwStudentMapper;

    @Autowired
    private BwClassMapper bwClassMapper;

    /**
     * 查询学生
     * 
     * @param id 学生主键
     * @return 学生
     */
    @Override
    public BwStudent selectBwStudentById(Long id)
    {
        return bwStudentMapper.selectBwStudentById(id);
    }

    /**
     * 查询学生列表
     * 
     * @param bwStudent 学生
     * @return 学生
     */
    @Override
    public List<BwStudent> selectBwStudentList(BwStudent bwStudent)
    {
        return bwStudentMapper.selectBwStudentList(bwStudent);
    }

    /**
     * 新增学生
     * 
     * @param bwStudent 学生
     * @return 结果
     */
    @Override
    public int insertBwStudent(BwStudent bwStudent)
    {
        bwStudent.setCreateTime(DateUtils.getNowDate());
//        BwClass bwClass = bwClassMapper.selectBwClassById(bwStudent.getClassId());
//
//        bwStudent.setCollegeId(bwClass.getCollegeId());
//        bwStudent.setSchoolId(bwClass.getSchoolId());
        List<BwStudent> list = bwStudentMapper.selectBwStudentNumber();
        String number = null;
        for (BwStudent student : list) {
             number = student.getStuNumber();
        }

        Integer integer = Integer.valueOf(number);
        integer ++;

        number = String.valueOf(integer);

        bwStudent.setStuNumber(number);
        return bwStudentMapper.insertBwStudent(bwStudent);
    }

    /**
     * 修改学生
     * 
     * @param bwStudent 学生
     * @return 结果
     */
    @Override
    public int updateBwStudent(BwStudent bwStudent)
    {
        bwStudent.setUpdateTime(DateUtils.getNowDate());
        CvInformationVo cvInformation = bwStudentMapper.selectCvInform(bwStudent.getId());
        if (cvInformation != null && !cvInformation.getStudentMobile().equals(bwStudent.getStuMobile())){
            bwStudentMapper.updateCvInform(bwStudent.getId(),bwStudent.getStuMobile());

        }
        return bwStudentMapper.updateBwStudent(bwStudent);
    }

    /**
     * 批量删除学生
     * 
     * @param ids 需要删除的学生主键
     * @return 结果
     */
    @Override
    public int deleteBwStudentByIds(Long[] ids)
    {
        return bwStudentMapper.deleteBwStudentByIds(ids);
    }

    /**
     * 删除学生信息
     * 
     * @param id 学生主键
     * @return 结果
     */
    @Override
    public int deleteBwStudentById(Long id)
    {
        return bwStudentMapper.deleteBwStudentById(id);
    }

    @Override
    public List<BwStudent> listById(Integer id) {

        return bwStudentMapper.listById(id);
    }

    @Override
    public List<BwStudent> listStu() {
        return bwStudentMapper.listStu();
    }

    @Override
    public SysDept selectDeptById(Long id) {
        return bwStudentMapper.selectDeptById(id);
    }

    @Override
    public BwClass selectClassById(Long id) {
        return bwStudentMapper.selectClassById(id);
    }

    @Override
    public int stuSave(SaveVo saveVo) {
        try {
            for (Integer id : saveVo.getId()) {
                BwClass bwClass = bwClassMapper.selectBwClassById(new Long(saveVo.getClassId()));
                bwStudentMapper.stuSave(id,saveVo.getClassId(),bwClass);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    @Override
    public List<SysUserRoleVo> selectRoleList(Long userId) {
        return bwStudentMapper.selectRoleList(userId);
    }

    @Override
    public BwStudent getStudentInfo(Long userId) {
        return bwStudentMapper.getStudentInfo(userId);
    }



}
